package xfuzzy.alg_genetico.indeterminado.view;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import xfuzzy.xfhl.model.hierStruct.XfhlModule;
import xfuzzy.xfhl.model.hierStruct.XfhlNode;

public class XfhlModuleToGraph {
	
//	public static void createGraph(DoubleChromosome chromosome, String filename, boolean reducida)  {
//		
//		XfhlModule module = Encoder.decodeBackwardAnchura(chromosome);
//		createGraph(module, filename, reducida);
//	}
	

	public static void createGraph(XfhlModule modulo, String filename, boolean reducida)  {
		
		List<XfhlModule> modulos = new ArrayList<XfhlModule>();
		modulos.add(modulo);
		XfhlModule actual = null;
		String output = "digraph G { " + "\n";
		if (reducida)
			output += "size =\"5,5\";" + "\n";
		output += "graph [bgcolor=gray];" + "\n";
		output += "node [style=bold, shape=record];" + "\n";
		output += "edge [color=blue];" + "\n";
		output += "graph [bb=\"0,0,393,252\"];" + "\n";
		String id = "";
		// bucle para crear las formas de la representacion
		while (modulos.size() > 0)  {
			actual = modulos.remove(0);
			id = String.valueOf(actual.getIdMod());
			output += "node" + id + " [ label=\" <ent1> Ent1 | <" + id + ">" + id + " | <ent2> Ent2 \", width=\"0.83\", height=\"0.50\"];" + "\n";
			if (actual.getEnt2() instanceof XfhlModule)  {
				modulos.add((XfhlModule) actual.getEnt2());
			}
			if (actual.getEnt2() instanceof XfhlNode)  {
				XfhlNode nodo = (XfhlNode) actual.getEnt2();
				id = String.valueOf(nodo.getValor());
				output += "node" + id + " [ label = \"input" + id + "\", shape = oval ];" + "\n";
			}
			
			if (actual.getEnt1() instanceof XfhlModule)  {
				modulos.add((XfhlModule) actual.getEnt1());
			}
			if (actual.getEnt1() instanceof XfhlNode)  {
				XfhlNode nodo = (XfhlNode) actual.getEnt1();
				id = String.valueOf(nodo.getValor());
				output += "node" + id + " [ label = \"input" + id + "\", shape = oval ];" + "\n";
			}
		}
		
		modulos.add(modulo);
		while (modulos.size() > 0)  {
			actual = modulos.remove(0);
			if (actual.getEnt2() instanceof XfhlModule)  {
				XfhlModule modulo2 = (XfhlModule) actual.getEnt2();
				output += "node" + String.valueOf(actual.getIdMod()) + ":ent2" + " -> " + "node" + String.valueOf(modulo2.getIdMod()) + ";\n";
				modulos.add(modulo2);
			}
			if (actual.getEnt2() instanceof XfhlNode)  {
				XfhlNode nodo = (XfhlNode) actual.getEnt2();
				output += "node" + String.valueOf(actual.getIdMod()) + ":ent2" + " -> " + "node" + nodo.getValor() + ";\n";
			}
			
			if (actual.getEnt1() instanceof XfhlModule)  {
				XfhlModule modulo2 = (XfhlModule) actual.getEnt1();
				output += "node" + String.valueOf(actual.getIdMod()) + ":ent1" + " -> " + "node" + String.valueOf(modulo2.getIdMod()) + ";\n";
				modulos.add(modulo2);
			}
			if (actual.getEnt1() instanceof XfhlNode)  {
				XfhlNode nodo = (XfhlNode) actual.getEnt1();
				output += "node" + String.valueOf(actual.getIdMod()) + ":ent1" + " -> " + "node" + nodo.getValor() + ";\n";
			}
		}
		output += "}" + "\n";
		try   {
			FileOutputStream fos = new FileOutputStream(filename + ".dot");
			PrintStream stream = new PrintStream(fos);
			stream.println(output);
			stream.close();
			String comando = "dot -Tpng " + filename + ".dot -o " + filename + ".png"; 
			Runtime.getRuntime().exec(comando);
		}
		catch(Exception e)  {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
}
